<template>
  <div class="tags-container">
    <el-card>
      <div v-if="this.$route.query.name" slot="header">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item>学科管理</el-breadcrumb-item>
          <el-breadcrumb-item>{{ this.$route.query.name }}</el-breadcrumb-item>
          <el-breadcrumb-item>标签管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <el-row>
        <el-col :span="18">
          <el-form
            :model="queryInfo"
            :inline="true"
            ref="form"
            label-width="80px"
          >
            <el-form-item label="标签名称" prop="tagName">
              <el-input
                v-model.trim="queryInfo.tagName"
                placeholder="根据标签名称搜索"
              ></el-input>
            </el-form-item>
            <el-form-item label="状态" prop="state">
              <el-select
                v-model="queryInfo.state"
                placeholder="请选择"
                @change="stateChange"
              >
                <el-option label="已启用" value="1"></el-option>
                <el-option label="已禁用" value="0"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button @click="clear">清除</el-button>
              <el-button type="primary" @click="getTagsList">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="6" class="add-btn">
          <el-button
            class="addNew"
            @click="addDialog"
            type="success"
            icon="el-icon-edit"
            >新增标签</el-button
          >
          <el-button
            v-if="this.$route.query.id"
            class="addNew"
            @click="$router.push('/subjects/list')"
            type="text"
            icon="el-icon-back"
            >返回学科</el-button
          >
        </el-col>
      </el-row>

      <el-alert :title="'数据一共' + counts + '条'" type="info" show-icon>
      </el-alert>

      <el-table :data="subjectsListAll" stripe style="width: 100%">
        <el-table-column prop="id" label="序号" width="80" type="index">
          <template scope="scope">
            <span
              >{{
                (queryInfo.page - 1) * queryInfo.pagesize + (scope.$index + 1)
              }}
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="subjectName" label="所属学科" width="180">
        </el-table-column>
        <el-table-column prop="tagName" label="标签名称"> </el-table-column>
        <el-table-column prop="username" label="创建者"> </el-table-column>
        <el-table-column prop="addDate" label="创建时间">
          <template slot-scope="scope">
            {{ scope.row.addDate | dateFormat("YYYY-mm-dd HH:MM:SS") }}
          </template>
        </el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            {{ scope.row.state ? "已启用" : "已禁用" }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              @click="changeState(scope.row.state, scope.row.id)"
              type="text"
              >{{ scope.row.state === 1 ? "禁用" : "启用" }}</el-button
            >
            <el-button
              type="text"
              @click="modDialog(scope.row.id)"
              :disabled="scope.row.state === 1"
              >修改</el-button
            >
            <el-button
              type="text"
              @click="delTags(scope.row.id)"
              :disabled="scope.row.state === 1 || scope.row.totals > 0"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        background
        layout="prev, pager, next, sizes, jumper"
        :total="counts"
        :page-size="queryInfo.pagesize"
        :current-page="queryInfo.page"
        @current-change="handleCurrentChange"
        :page-sizes="[5, 10, 20, 50]"
        @size-change="handleSizeChange"
        hide-on-single-page
      ></el-pagination>
    </el-card>

    <el-dialog title="新增标签" :visible.sync="addDialogVisible" width="400px">
      <el-form ref="addform" :rules="rules" :model="addform" label-width="80px">
        <el-form-item
          v-if="!this.$route.query.id"
          label="所属学科"
          prop="subjectID"
        >
          <el-select
            v-model="addform.subjectID"
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="item in subjectsList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标签名称" prop="tagName">
          <el-input v-model.trim="addform.tagName"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addTages">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog title="修改标签" :visible.sync="modDialogVisible" width="400px">
      <el-form ref="modform" :rules="rules" :model="modform" label-width="80px">
        <el-form-item
          v-if="!this.$route.query.id"
          label="所属学科"
          prop="subjectID"
        >
          <el-select
            v-model="modform.subjectID"
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="item in subjectsList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标签名称" prop="tagName">
          <el-input v-model.trim="modform.tagName"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="modDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="modTages">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import dateFormat from "@/utils/dateFormat.js";
import {
  list,
  add,
  changeState,
  remove,
  detail,
  update,
} from "@/api/hmmm/tags.js";
import { simple } from "@/api/hmmm/subjects.js";

export default {
  name: "tags",

  data() {
    return {
      subjectsListAll: [],
      subjectsList: [],
      rules: {
        tagName: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
        ],
      },
      queryInfo: {
        subjectID: null,
        tagName: null,
        state: null,
        page: 1,
        pagesize: 10,
      },
      counts: 0,
      addDialogVisible: false,
      modDialogVisible: false,
      addform: {
        subjectID: null,
        tagName: "",
      },
      modform: {
        id: "",
        subjectID: null,
        tagName: "",
      },
    };
  },

  created() {
    this.getTagsList();
  },

  watch: {
    $route(val) {
      this.subjectID = val.query.id || null;
      this.getTagsList();
    },
  },

  filters: {
    dateFormat(val, format) {
      const date = new Date(val);
      return dateFormat(format, date);
    },
  },

  methods: {
    //选择
    stateChange() {
      this.getTagsList();
    },

    // 删除
    delTags(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          try {
            await remove({ id });
            this.$message.success("删除成功");
            this.getTagsList();
          } catch (err) {
            this.$message.error("删除失败");
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    // 修改
    async modDialog(id) {
      this.modDialogVisible = true;
      this.modform.id = id;
      try {
        const { data } = await detail({ id });
        this.modform = data;
      } catch (err) {
        this.$message.error("获取失败");
      }
      try {
        const { data } = await simple();
        this.subjectsList = data;
      } catch (error) {
        s;
        this.$message.error("获取失败");
      }
    },

    // 点击修改
    modTages() {
      let flag;
      this.$refs.modform.validate((valid) => {
        flag = valid;
      });
      if (!flag) return;
      try {
        update(this.modform);
        this.$message.success("修改成功");
        this.getTagsList();
        this.modDialogVisible = false;
      } catch (err) {
        this.$message.error("修改失败");
        this.modDialogVisible = false;
      }
    },

    // 切换状态
    async changeState(state, id) {
      try {
        await changeState({
          id: id,
          state: state ? 0 : 1,
        });
        this.$message.success("更新成功");
        this.getTagsList();
      } catch (err) {
        this.$message.error("更新失败");
      }
    },

    // 新增
    async addDialog() {
      this.addDialogVisible = true;
      try {
        const { data } = await simple();
        this.subjectsList = data;
      } catch (err) {}
    },

    // 点击新增
    async addTages() {
      this.addDialogVisible = false;
      if (!this.addform.subjectID) this.addform.subjectID = 0;
      try {
        await add(this.addform);
        this.$message.success("添加成功");
        this.getTagsList();
        this.$refs.addform.resetFields();
      } catch (err) {
        this.$message.error("添加失败");
        this.addDialogVisible = false;
      }
    },

    // 获取列表
    async getTagsList() {
      try {
        const { data } = await list(this.queryInfo);
        this.subjectsListAll = data.items;
        this.counts = data.counts;
      } catch (err) {
        this.$message.error("获取列表失败!");
      }
    },

    // 清除
    clear() {
      (this.queryInfo = {
        tagName: null,
        state: null,
        page: 1,
        pagesize: 10,
      }),
        this.getTagsList();
    },

    // 切换条数
    handleSizeChange(newSize) {
      this.queryInfo.page = 1;
      this.queryInfo.pagesize = newSize;
      this.getTagsList();
    },

    // 切换分页
    handleCurrentChange(newPage) {
      this.queryInfo.page = newPage;
      this.getTagsList();
    },
  },
};
</script>

<style scoped lang='less'>
.tags-container {
  .addNew {
    float: right;
    margin-left: 10px;
  }

  /deep/ .el-dialog__footer {
    text-align: right;
  }

  .el-pagination {
    margin-top: 15px;
    text-align: right;
  }
  .el-alert--info {
    margin-bottom: 15px;
  }

  .el-form--inline {
    height: 51px;
  }
}
</style>
